package com.niubi.yygh.cmn.controller;

import com.niubi.yygh.cmn.service.DictService;
import com.niubi.yygh.common.result.R;
import com.niubi.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(tags = "数据字典管理")
@RestController
@RequestMapping("/admin/cmn/dict")
//@CrossOrigin
public class DictController{
  @Autowired
  private DictService dictService;

  @ApiOperation("数据字典列表")
  @GetMapping("findChildData/{id}")
  public R findChildData (@PathVariable Long id){
    List<Dict> list = dictService.findChlidData(id);
    return R.ok().data("list",list);
  }

  @ApiOperation("数据字典导出")
  @GetMapping("/exportData")
  public void exportData(HttpServletResponse response){
    dictService.exportData(response);
  }

  @ApiOperation("数据字典导入")
  @PostMapping("importData")
  public R importData(MultipartFile file){
    dictService.importDictData(file);
    return R.ok();
  }

  @ApiOperation(value = "获取数据字典名称")
  @GetMapping(value = "/getName/{parentDictCode}/{value}")
  public String getName(@PathVariable("parentDictCode") String parentDictCode,
                        @PathVariable("value") String value){
    return dictService.getNameByCode(value,parentDictCode);
  }

  @ApiOperation(value = "获取数据字典名称")
  @GetMapping(value = "/getName/{value}")
  public String getName(@PathVariable("value") String value){
    return dictService.getNameByCode(value,"");
  }

  //查询所有数据（所有省、所有医院等级、所有民族）
  @ApiOperation(value = "根据dictCode获取下级节点")
  @GetMapping(value = "/findByDictCode/{dictCode}")
  public R findByDictCode(
          @PathVariable String dictCode){
      List<Dict> list = dictService.getByDictCode(dictCode);
      return R.ok().data("list",list);
  }
}